﻿
//#define TEST
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Bluebunnie.Extensions;
using Bluebunnie.Library.Master;
using Bluebunnie.Library.Transaction;
using Fluent;
using System.Collections.ObjectModel;

namespace Bluebunnie
{
    public partial class MainWindow : RibbonWindow, IShell
    {
        ObservableCollection<TabItem> _tabs = new ObservableCollection<TabItem>();

        public MainWindow()
        {
            InitializeComponent();
            App.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
            App.Current.MainWindow = this;

            _content.ItemsSource = _tabs;
            Shell.Current = this;

            Show("Dashboard", new Dashboard());


#if TEST
            bool? x = new Login().ShowDialog();

            if (x != true)
                App.Current.Shutdown(0);
#endif
        }

        public void Echo(string text)
        {
            _txtEcho.Text = text;
        }

        public void Show(string title, Window w)
        {
            var tab = _tabs.FirstOrDefault(x => (x.Header as TabHeader).Text.Equals(title));

            if (tab != null)
            {
                _content.SelectedItem = tab;
                return;
            }

            object e = w.Content;
            w.Content = null;
            UserControl uc = new UserControl();
            uc.Content = e;

            var item = new CustomTabItem { Header = new TabHeader(w.Title), Content = uc };
            _tabs.Add(item);
            _content.SelectedItem = item;
        }

        public void Show(string title, UserControl uc)
        {
            var tab = _tabs.FirstOrDefault(x => (x.Header as TabHeader).Text.Equals(title));

            if (tab != null)
            {
                _content.SelectedItem = tab;
                return;
            }

            var item = new CustomTabItem { Header = new TabHeader(title), Content = uc };
            _tabs.Add(item);
            _content.SelectedItem = item;
        }

        public void Show(string title, PulseControl uc)
        {
            var tab = _tabs.FirstOrDefault(x => (x.Header as TabHeader).Text.Equals(title));

            if (tab != null)
            {
                _content.SelectedItem = tab;
                return;
            }

            var item = new CustomTabItem { Header = new TabHeader(title, uc.Save), Content = uc };
            _tabs.Add(item);
            _content.SelectedItem = item;

            uc.Language = XmlLanguage.GetLanguage("id-ID");
        }

        public void Close(string title)
        {
            var tab = _tabs.FirstOrDefault(x => (x.Header as TabHeader).Text.Equals(title));
            if (tab != null)
                _tabs.Remove(tab);
        }
        
        private void btnDaftarItem_Click(object sender, RoutedEventArgs e)
        {
            Show("Data Barang", new Barang());
        }

        private void btnSupplier_Click(object sender, RoutedEventArgs e)
        {
            Show("Data Supplier", new Supplier());
        }

        private void btnPelanggan_Click(object sender, RoutedEventArgs e)
        {
            Show("Data Pelanggan", new Customer());
        }

        private void btnSales_Click(object sender, RoutedEventArgs e)
        {
            Show("Data Sales", new Sales());
        }

        private void btnUser_Click(object sender, RoutedEventArgs e)
        {
            Show("Data User", new Users());
        }

        private void btnSetingKas_Click(object sender, RoutedEventArgs e)
        {
            Show("Setting Kas", new SettingKas());
        }

        private void btnKasKeluarMasuk_Click(object sender, RoutedEventArgs e)
        {
            Show("Kas Keluar Masuk", new CatatanKasHistoriKas());
        }

        private void btnPesananBeli_Click(object sender, RoutedEventArgs e)
        {
            Show("Pesanan Pembelian", new PurchaseOrder());
        }

        private void btnPembelian_Click(object sender, RoutedEventArgs e)
        {
            Show("Data Pembelian", new PurchaseTransaction());
        }

        private void btnItemMasuk_Click(object sender, RoutedEventArgs e)
        {
            Show("Daftar Item Masuk", new PurchaseItemIn());
        }

        private void btnRekturPembelian_Click(object sender, RoutedEventArgs e)
        {
            Show("Retur Pembelian", new PurchaseReturn());
        }

        private void btnPembayaranHutang_Click(object sender, RoutedEventArgs e)
        {
            Show("Pembayaran Hutang", new PurchaseDebt());
        }

        private void btnPesananJual_Click(object sender, RoutedEventArgs e)
        {
            Show("Pesanan Penjualan", new SaleOrder());
        }

        private void btnPenjualan_Click(object sender, RoutedEventArgs e)
        {
            Show("Data Penjualan", new SaleTransaction());
        }

        private void btnItemKeluar_Click(object sender, RoutedEventArgs e)
        {
            Show("Item Keluar", new SaleItemOut());
        }

        private void btnReturPenjualan_Click(object sender, RoutedEventArgs e)
        {
            Show("Retur Penjualan", new SaleReturn());
        }

        private void btnPembayaranPiutang_Click(object sender, RoutedEventArgs e)
        {
            Show("Pembayaran Piutang", new SaleClaim());
        }

        private void btnLaporanDaftarItem_Click(object sender, RoutedEventArgs e)
        {
            Show("[Report] Barang", new ReportViewer(new ReportBarangViewer()));
        }

        private void btnLevelHarga_Click(object sender, RoutedEventArgs e)
        {
            Show("Level Harga", new PriceLevel());
        }

        private void btnLaporanSuplier_Click(object sender, RoutedEventArgs e)
        {
            Show("[Report] Supplier", new ReportViewer(new ReportSupplierViewer()));
        }

        private void btnLaporanPelanggan_Click(object sender, RoutedEventArgs e)
        {
            Show("[Report] Customer", new ReportViewer(new ReportCustomerViewer()));
        }

        private void btnLaporanSales_Click(object sender, RoutedEventArgs e)
        {
            Show("[Report] Sales", new ReportViewer(new ReportSalesViewer()));
        }

        private void btnLaporanItemMasuk_Click(object sender, RoutedEventArgs e)
        {
            Show("[Report] Item Masuk", new ReportViewer(new ReportItemInViewer()));
        }

        private void btnLaporanItemKeluar_Click(object sender, RoutedEventArgs e)
        {
            Show("[Report] Item Keluar", new ReportViewer(new ReportItemOutViewer()));
        }

        private void btnLaporanStock_Click(object sender, RoutedEventArgs e)
        {
            Show("[Report] Stock", new ReportViewer(new ReportStock()));
        }

        private void btnLaporanPesananPembelian_Click(object sender, RoutedEventArgs e)
        {
            Show("[Report] Pesanan Pembelian", new ReportViewer(new ReportPurchaseOrder()));
        }

        private void btnLaporanPembelian_Click(object sender, RoutedEventArgs e)
        {
            Show("[Report] Transaksi Pembelian", new ReportViewer(new ReportPurchase()));
        }

        private void btnLaporanReturPembelian_Click(object sender, RoutedEventArgs e)
        {
            Show("[Report] Retur Pembelian", new ReportViewer(new ReportPurchaseReturn()));
        }

        private void btnLaporanPesananPenjualan_Click(object sender, RoutedEventArgs e)
        {
            Show("[Report] Pesanan Penjualan", new ReportViewer(new ReportSaleOrder()));
        }

        private void btnLaporanPenjualan_Click(object sender, RoutedEventArgs e)
        {
            Show("[Report] Pesanan Penjualan", new ReportViewer(new ReportPurchase()));
        }

        private void btnLaporanReturPenjualan_Click(object sender, RoutedEventArgs e)
        {
            Show("[Report] Retur Penjualan", new ReportViewer(new ReportPurchaseReturn()));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            new SqlExecutor().ShowDialog();
        }

        private void btnDashboard_Click(object sender, RoutedEventArgs e)
        {
            Show("Dashboard", new Dashboard());
        }
        
    }
         
}